package com.h.forum.controller;

import com.h.forum.common.constant.Constants;
import com.h.forum.common.enums.ResultCode;
import com.h.forum.common.utils.JWTUtil;
import com.h.forum.pojo.model.Article;
import com.h.forum.pojo.model.ArticleReply;
import com.h.forum.pojo.request.ArticleReplyCreateRequest;
import com.h.forum.pojo.response.AppResult;
import com.h.forum.pojo.response.UserInfoResponse;
import com.h.forum.services.IArticleReplyService;
import com.h.forum.services.IArticleService;
import com.h.forum.services.IUserService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource(name = "articleReplyServiceImpl")
    private IArticleReplyService articleReplyService;

    @Resource(name = "userServiceImpl")
    private IUserService userService;

    @Resource(name = "articleServiceImpl")
    private IArticleService articleService;

    /**
     * 创建回复的信息
     * @param request
     * @return
     */
    @PostMapping("/create")
    public AppResult create(@Validated @RequestBody ArticleReplyCreateRequest request) {
        // 获取用户信息，校验用户状态
        // 校验用户的状态
        Long userId = JWTUtil.getUserIdFromToken(Constants.JWT);
        // 获取对应的用户信息
        UserInfoResponse userInfo = userService.selectById(userId);
        if (userInfo.getState().equals(Constants.IS_STATUS_STATE)) {
            // 用户不能进行修改
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        // 获取需要回复的帖子，进行校验
        Article article = articleService.selectById(request.getArticleId());
        if (article == null
                || article.getDeleteState().equals(Constants.IS_DELETE_STATE)) {
            // 为空，没有帖子
            // 返回错误描述
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 判断帖子的状态，是否是已经归档
        if (article.getState().equals(Constants.IS_STATUS_STATE)) {
            // 进行返回，不能访问
            return AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED);
        }

        // 创建输入的对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(request.getArticleId()); // 帖子的Id
        articleReply.setPostUserId(userId); // 发送信息的对象
        articleReply.setContent(request.getContent()); // 评论的内容

        // 调用Service层
        articleReplyService.create(articleReply);

        return AppResult.success();
    }

    @GetMapping("/getReplies")
    private AppResult<List<ArticleReply>> getReplies(
            @RequestParam(value = "articleId") @NotNull Long articleId) {
        // 检验
        Article article = articleService.selectById(articleId);
        if (article == null
                || article.getDeleteState().equals(Constants.IS_DELETE_STATE)) {
            // 返回错误提示
            return AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        // 调用Service层
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);

        return AppResult.success(articleReplies);
    }
}
